﻿using NB.MGJ.Common;
using NB.MGJ.Common.API;
using NB.MGJ.Common.Cache;
using NB.MGJ.Common.Helper;
using NB.MGJ.Common.Utility;
using NB.MGJ.Model;
using NB.MGJ.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using Common;
namespace NB.MGJ.API
{
    /// <summary>
    /// 登录
    /// </summary>
    //[ThirdAuthorizeFilter]
    [ApiHandleError]
    public class ApiMGJAuthController : ApiBaseController<LogOnUserModel>
    {
        private ManagerService _mservice;
        public ApiMGJAuthController()
        {
            _mservice = new ManagerService();
        }

        //[Route("nbapi/pv/uv")]
        //[HttpGet]
        //public IHttpActionResult Test_Pv_Uv()
        //{
        //    string _ip = System.Web.HttpContext.Current.Request.UserHostAddress;
        //    Sys_PV_UV_Service.PV_UV_Add(_ip, "LogOn");
        //    //new Sys_PV_UV_Service().PV_UV_Add(_ip, "LogOn");
        //    return ApiSuccess(true);
        //}


        [Route("nbapi/auth/userlogon")]
        [HttpPost]
        public IHttpActionResult UserLogon([FromBody]LogOnModel _logonInfo)
        {
            var _userInfo = new HomeService().WX_UserLogin(_logonInfo);
            if (_userInfo == null)
            {
                return ApiFail("登录失败,检查用户名或密码是否正确");
            }
            var _token = MD5Manager.Gen_MD5(_userInfo.Pwd + DateTime.UtcNow.ToString("yyyyMMdd") + MD5Manager.GetNonCryptoRandomDataAsBase64(128), false, false);
            var _sign = MD5Manager.GetNonCryptoRandomDataAsBase64(16);
            return ApiSuccess(new
            {
                userInfo = UserHelperForApi.SetUserToSession(_userInfo, _token, _sign),
                token = _token,
                sign = _sign,
            });
        }




        [Route("nbapi/auth/companyinfo")]
        [HttpPost]
        public IHttpActionResult companyRegister([FromBody]Organization_Enterprise model)
        {
            model.ChangeDate = DateTime.Now;
            model.ViewDel = 0;
            model.ViewFlag = 1;
       
            int eId = _mservice.AddEnterpriseService(model);
            model.ID = eId;
            model.Code = eId.ToString().PadLeft(4, '0');
            _mservice.updEnterpriseService(model);
            //添加用户信息
            return ApiSuccess(addUserInfo(model.LoginName, model.Pwd, model.LoginName, eId, model.PhoneNumber, 1));
        }


        [Route("nbapi/auth/customerinfo")]
        [HttpPost]
        public IHttpActionResult customerRegister([FromBody]Sys_User model)
        {
            if (_mservice.GetUserInfoByName(model.LoginName) != null) return ApiFail("登录名已存在");

            //添加用户信息
            object obj = addUserInfo(model.LoginName, model.Pwd, model.LoginName, 0, model.Modile, 2);

            return ApiSuccess(obj);
        }

        public Object addUserInfo(string userName, string pwd, string contacts, int eId, string phoneNumber, int type)
        {
            L.EntityModel.Sys_User userInfo = new L.EntityModel.Sys_User();
            userInfo.ViewFlag = 1;
            userInfo.ViewDel = 0;
            userInfo.LoginName = userName;
            userInfo.Pwd = EDS3.DESEnCode(pwd);
            userInfo.EnterpriseId = eId;
            userInfo.RealName = contacts;
            userInfo.ChangeDate = DateTime.Now;
            userInfo.ChangeLoginID = 0;
            userInfo.ChangeLoginName = userName;
            userInfo.Modile = phoneNumber;
            userInfo.Remark = "小程序添加";
            userInfo.Type = type;
            _mservice.OrganAddUserInfoService(userInfo);
            return new { loginName = userInfo.LoginName, pwd = userInfo.Pwd };
        }

        //根据手机号获取验证码
        [Route("nbapi/auth/getverificationcode")]
        [HttpGet]
        public IHttpActionResult sendSMS(string phoneNumber, string name)
        {
            if (!String.IsNullOrWhiteSpace(phoneNumber) && !String.IsNullOrWhiteSpace(name))
            {
                SMSApi smsApi = new SMSApi();
                string str = smsApi.SendSMSApi("81a498a753448f368a5ad26555c165bc", phoneNumber, "41978", name);
                return ApiSuccess(str);
            }
            else
            {
                return ApiFail("验证码发送失败");
            }
        }
        [Route("nbapi/auth/postname")]
        [HttpGet]
        public IHttpActionResult checkLoginName(string loginName)
        {

            if (!string.IsNullOrWhiteSpace(loginName))
            {
                var model = _mservice.GetUserInfoByName(loginName);
                if (model == null) return ApiSuccess(true);
                else return ApiFail("登录名已存在");
            }
            else return ApiFail("登录名不能为空");
        }

    }
}
